﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xww.LCV.Model;
using Xww.LCV.Model.Graphics;

/*
namespace Xww.LCV.Generator {
    internal static class ThresholdRelativeGeneratorHelper {
        public static List<LCS> ExtractLCVs(this CharDirectedGraph Graphic, IEnumerable<CharSequence> Sequences, MiningParam param) {
            List<LCS> rst = new List<LCS>();
            int[] indexArray;
            ICollection<LCS> stepLCSs = new HashSet<LCS>(Graphic.GetSingleUnitLCSs(Sequences, param, out indexArray), LCS.InstanceForHash);
            rst.AddRange(stepLCSs);

            while (stepLCSs.Count > 1) {
                var groups = stepLCSs.GroupBy(o => o.SubFirstLCS, LCS.InstanceForHash);
                ICollection<LCS> currentLCS = new List<LCS>();
                foreach (var group in groups) {
                    var sortedGroup = group.ToArray();
                    for (int i = 0; i < sortedGroup.Length-1; i++) {
                        LCS the = sortedGroup[i];
                        for (int j = i+1; j < sortedGroup.Length; j++) {
                            LCS other = sortedGroup[j];
                            if (the.LastLocation == other.LastLocation)
                                continue;
                            LCS big = LCS.Imerge(the, other);
                            big.Support = Graphic[big[0].AA].CountOfNode(big);
                            if (big.Support >= param.Threshold)
                                currentLCS.Add(big);
                        }
                    }
                }
                rst.AddRange(currentLCS);
                stepLCSs = currentLCS;
            }

            return rst;
        }

        private static HashSet<LCS> GetSingleUnitLCSs(this CharDirectedGraph Graphic, IEnumerable<CharSequence> listPaths, MiningParam param, out int[] index) {
            HashSet<LCS> noLCSs = new HashSet<LCS>(LCS.InstanceForHash);
            HashSet<LCS> rst = new HashSet<LCS>(LCS.InstanceForHash);
            // 查找第一步骤的 LCV，所有长度为2中间可以连续或间隔
            foreach (CharSequence item in listPaths) {
                var pointer = item.First; // 长度为二的 LCS 的首个
                while (pointer.CheckNextNodeNull(1)) {
                    var startNode = pointer;
                    pointer = pointer.Next;

                    //int position = 2;
                    LCSUnit unit = new LCSUnit(startNode.Value, 1);
                    //todo : 设置最长跨度
                    var other = startNode.Next;
                    for (int position=2; other != null; ++position, other = other.Next) {
                        //
                        int interval = position - 1;
                        if (interval > param.MaxInterval) break;
                        if (interval < param.MinInterval) continue;
                        if (startNode.Value == CharSequence.START || other.Value == CharSequence.END) continue;
                        //
                        LCS lcs = new LCS(0, unit, new LCSUnit(other.Value, position));
                        if (rst.Contains(lcs) || noLCSs.Contains(lcs)) { continue; } // 如果有重复计算，则不再重新统计支持数
                        lcs.Support = Graphic[startNode.Value].CountOfNode(interval, other.Value);
                        if (lcs.Support >= param.Threshold)
                            rst.Add(lcs);
                        else
                            noLCSs.Add(lcs);
                    }
                }
            }
            noLCSs = null;
            rst = new HashSet<LCS>(rst.OrderBy(o => o));
            index = new int[0];
            return rst;
        }

    }
}
*/